import http from './http'
import { useAuthStore } from '@/stores/auth'
import { useRoleStore } from '@/stores/role'
import { useUserStore } from '@/stores/user'
import { apiMsgProp } from '@/utils/global'
import { useRoute, useRouter } from 'vue-router'

const AuthService = {
    /**
     * @function login 登录
     * @param credentials 登录数据
     */
    login: async (credentials: { account: string; password: string }) => {
        const res = await http.post('/course/auth/login', credentials)
        if (res.data.code === 200) {
            const { token, user, token_expired_at } = res.data.data

            const roleStore = useRoleStore()
            useAuthStore().setToken(token, token_expired_at)
            useUserStore().userinfoL = user
            await roleStore.fetchRoles()
            await roleStore.setCurrentRole(user.role_id)
        } else {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    /**
     * @function logout 登出
     */
    logout: async () => {
        const res = await http.post('/course/user/logout')
        apiMsgProp(res.data.code, res.data.message)
        useAuthStore().clearToken()
        useRoleStore().setCurrentRole(null)
        const userStore = useUserStore()
        localStorage.setItem(`redirectPath_${userStore.userinfoL?.id}`, window.location.pathname + window.location.search)
        window.location.href = '/'

        
    },
    /**
     * @function refreshToken 刷新token
     */
    refreshToken: async () => {
        const res = await http.post('/course/user/refresh-token')
        if (res.data.code === 200) {
            const { token, token_expired_at } = res.data.data
            useAuthStore().setToken(token, token_expired_at)
        } else {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    /**
     * @function updatePassword 重置密码
     * @param data 重置密码数据
     */
    updatePassword: async (data: { id: number; old_password: string; password: string; confirm_password: string }): Promise<void> => {
        const res = await http.post('/course/auth/reset-password', data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) {
            return Promise.reject(res.data)
        }
    },
    /**
     * @function searchAccount 搜索用户
     * @param account 用户名
     */
    searchAccount: async (data: { account?: string; email?: string; send_email: boolean }): Promise<void> => {
        const res = await http.post('/course/auth/forget-password', data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
    /**
     * @function verifyToken 验证token
     * @param token 令牌
     */
    verifyToken: async (token: string): Promise<any> => {
        const res = await http.post('/course/auth/verify-reset-password-token', { token })
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)

        return res.data
    },
    /**
     * @function resetPassword 重置密码
     * @param data 重置密码数据
     */
    resetPassword: async (data: { id: number; password: string; confirm_password: string; token: string }): Promise<void> => {
        const res = await http.post('/course/auth/reset-password', data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
}

export default AuthService
